﻿using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using System.Collections;
using System.Web.Security;
using System.Windows.Forms;

public partial class main1 : System.Web.UI.Page
{

    string strconn = ConfigurationManager.ConnectionStrings["a123"].ToString();
    SqlConnection sqlconn;
    string text;
    protected void Page_Load(object sender, EventArgs e)
    {
        text = Request.QueryString["id"];
        if (!IsPostBack)
        {
            bind();
            if (text == "00" || text==null)
            {
                bind();
             }
             else {
                 int a = Convert.ToInt32(text);
                aa(a);
            }
        }
        
        
    }
    public void bind()
    {
        sqlconn = new SqlConnection(strconn);
        sqlconn.Open();
        string sqlstr = "select top 1 * from tb_BookInfo where Recommend='true'";
        SqlDataAdapter myda = new SqlDataAdapter(sqlstr, sqlconn);
        DataSet myds = new DataSet();
        myda.Fill(myds, "BookID");

        string sqlstr1 = "select top 1 * from tb_BookInfo where Discount='true'";
        SqlDataAdapter myda1 = new SqlDataAdapter(sqlstr1, sqlconn);
        DataSet myds1 = new DataSet();
        myda1.Fill(myds1, "BookID");

        string sqlstr2 = "select top 1 * from tb_BookInfo where Sell_well='true'";
        SqlDataAdapter myda2 = new SqlDataAdapter(sqlstr2, sqlconn);
        DataSet myds2 = new DataSet();
        myda2.Fill(myds2, "BookID");


        GridView2.DataSource = myds;
        GridView2.DataKeyNames = new string[] { "BookID" };
        GridView2.DataBind();
        GridView1.DataSource = myds1;
        GridView1.DataKeyNames = new string[] { "BookID" };
        GridView1.DataBind();
        GridView3.DataSource = myds2;
        GridView3.DataKeyNames = new string[] { "BookID" };
        GridView3.DataBind();
        sqlconn.Close();
        for (int i = 0; i <= GridView2.Rows.Count - 1; i++)
        {
            System.Web.UI.WebControls.Label label = (System.Web.UI.WebControls.Label)GridView2.Rows[i].FindControl("Label2");
            System.Web.UI.WebControls.HyperLink hyperlink1 = (System.Web.UI.WebControls.HyperLink)GridView2.Rows[i].FindControl("HyperLink5");
            //string number = GridView1.Rows[0].Cells[1].Text;
            hyperlink1.NavigateUrl = "~/main1.aspx?id=" + GridView2.DataKeys[i].Value;
        }
        for (int i = 0; i <= GridView1.Rows.Count - 1; i++)
        {
            System.Web.UI.WebControls.Label label = (System.Web.UI.WebControls.Label)GridView1.Rows[i].FindControl("Label2");
            System.Web.UI.WebControls.HyperLink hyperlink1 = (System.Web.UI.WebControls.HyperLink)GridView1.Rows[i].FindControl("HyperLink6");
            //string number = GridView1.Rows[0].Cells[1].Text;
            hyperlink1.NavigateUrl = "~/main1.aspx?id=" + GridView1.DataKeys[i].Value;
        }
        for (int i = 0; i <= GridView3.Rows.Count - 1; i++)
        {
            System.Web.UI.WebControls.Label label2 = (System.Web.UI.WebControls.Label)GridView3.Rows[i].FindControl("Label2");
            System.Web.UI.WebControls.Label label3 = (System.Web.UI.WebControls.Label)GridView3.Rows[i].FindControl("Label3");
            System.Web.UI.WebControls.Label label4 = (System.Web.UI.WebControls.Label)GridView3.Rows[i].FindControl("Label4");
            System.Web.UI.WebControls.Image image = (System.Web.UI.WebControls.Image)GridView3.Rows[i].FindControl("Image1");
            System.Web.UI.WebControls.HyperLink hyperlink1 = (System.Web.UI.WebControls.HyperLink)GridView3.Rows[i].FindControl("HyperLink7");
            //System.Web.UI.WebControls.Button button = (System.Web.UI.WebControls.Button)GridView3.Rows[i].FindControl("Button1");
            //System.Web.UI.WebControls.Button button = (System.Web.UI.WebControls.Button)GridView3.Rows[i].FindControl("Button1");
            //string number = GridView1.Rows[0].Cells[1].Text;
            //hyperlink1.NavigateUrl =  + GridView3.DataKeys[i].Value;
            int A = int.Parse(label2.Text);
            float B = float.Parse(label4.Text);

            hyperlink1.NavigateUrl = "~/main1.aspx?id="+GridView3.DataKeys[i].Value;
            
        }


    }
    public void aa(int ID)
    {
        if (Session["ID"] == null)
        {
            DialogResult result = MessageBox.Show("你还没登陆，是否登陆？", "信息提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2, MessageBoxOptions.ServiceNotification);
            if (result == DialogResult.OK)
            {
                Response.Redirect("login.aspx");
            }
            else { DialogResult.Cancel.Equals("true"); }
        }
        else
        {
            string s1 = Session["ID"].ToString();
            DataClassesDataContext db = new DataClassesDataContext();
            db.insert_shopping(ID, s1);
        }
    }
}